//
//  ViewController_iPhone.m
//  ICE_Player
//
//  Created by xl on 11-6-22.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ViewController_iPhone.h"
#import "Globalize.h"
#include "ICE_global_var.h"
#include "../Functions/file_list.h"

#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>


void copy_html_to_dst_folder(char* src_folder, char* dst_folder)
{
    DIR* dir = NULL;
    char ice_img[MAX_FILE_NAME_LEN] = {0};
    char tmp_src_path[MAX_FILE_NAME_LEN] = {0};
    char tmp_path[MAX_FILE_NAME_LEN] = {0};
    
    char* up_level_file[] = {/*STR_FILE_DIR_XML,*/ STR_INDEX_HTML};
    char* old_img_level_file[] = {STR_OLD_CONTANT_BG_PNG, STR_OLD_HTML_BG_JPG, STR_OLD_LIST_BG_PNG, STR_OLD_LIST_FOCUS_PNG, STR_OLD_LOGO_PNG, STR_OLD_SIDEBAR1_BG_PNG, STR_OLD_W_AUDIO_PNG, STR_OLD_W_MOVIE_PNG, STR_OLD_W_OTHER_PNG, STR_OLD_W_PICTURE_PNG};
    char* img_level_file[] = {STR_CONTANT_BG_PNG, STR_HTML_BG_JPG, STR_LIST_BG_PNG, STR_LIST_FOCUS_PNG, STR_LOGO_PNG, STR_SIDEBAR1_BG_PNG, STR_W_AUDIO_PNG, STR_W_MOVIE_PNG, STR_W_OTHER_PNG, STR_W_PICTURE_PNG};
    int i = 0;
    
    // make directories
    if (!(dir = opendir(dst_folder)))
    {
        mkdir(dst_folder, 0777);
    }
    if (dir)
    {
        closedir(dir);
        dir = NULL;
    }
    sprintf(ice_img, "%s%s", dst_folder, STR_ICE_IMG);
    if (!(dir = opendir(ice_img)))
    {
        mkdir(ice_img, 0777);
    }
    if (dir)
    {
        closedir(dir);
        dir = NULL;
    }
    
    // copy pages
    for (i = 0; i < (sizeof(up_level_file)/sizeof(up_level_file[0])); ++i)
    {
        sprintf(tmp_src_path, "%s%s", src_folder, up_level_file[i]);
        sprintf(tmp_path, "%s%s", dst_folder, up_level_file[i]);
        create_copy_file(tmp_src_path, tmp_path);
    }
    
    // copy images
    for (i = 0; i < (sizeof(img_level_file)/sizeof(img_level_file[0])); ++i)
    {
        sprintf(tmp_src_path, "%s%s", src_folder, old_img_level_file[i]);
        sprintf(tmp_path, "%s%s", ice_img, img_level_file[i]);
        create_copy_file(tmp_src_path, tmp_path);
    }
}

@interface ViewController_iPhone(PrivateMethod)
@end


@implementation ViewController_iPhone

@synthesize bg_view;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
    //init password module
    if (!psw)
    {
        psw = [[Password_Mgr alloc] init];
    }
    bg_view.psw_mgr = psw;
    
    //Load configue
    ICE_CONFIG* p_cfg = gf_get_ice_cfg();
    [ICE_Hidable_Label_View Enable_Hide_All:!p_cfg->enable_text_info];
    
    /*UIApplication* app = [UIApplication sharedApplication];
	[app setStatusBarHidden:TRUE withAnimation:UIStatusBarAnimationSlide];
	self.view.frame = [UIScreen mainScreen].bounds;*/
    
	//Add all sub view
	[self.view addSubview:bg_view];
    bg_view.player_view_controller.view.hidden = TRUE;
    [self.view addSubview:bg_view.player_view_controller.view];
	[bg_view create_SubViews:self];
    
    // start web server
    if (!g_web_server_handle)
    {
        char src_path[MAX_FILE_NAME_LEN] = {0};
        char t_path[MAX_FILE_NAME_LEN] = {0};
        
        [[[NSBundle mainBundle] resourcePath] getCString:src_path];
        strcat(src_path, "/");
        
        [NSHomeDirectory() getCString:t_path];
        g_web_server_html_path = malloc(MAX_FILE_NAME_LEN);
        memset(g_web_server_html_path, 0, MAX_FILE_NAME_LEN);
        sprintf(g_web_server_html_path, "%s%s", t_path, WEB_FOLDER);
    
        copy_html_to_dst_folder(src_path, g_web_server_html_path);
        
        [NSHomeDirectory() getCString:src_path];
		strcat(src_path, FILE_FOLDER);
        web_server_mg_set_redir_path(WEB_REDIR_SYMBOL, src_path);
        
        g_web_server_handle = web_server_start(g_web_server_html_path, NULL);
    }
    
    //set lock button state
    bg_view.lock_files_btn.state = (gf_get_ice_cfg()->is_files_locked ? E_LBI_LOCK : E_LBI_UNLOCK);
}


///*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    if (gf_get_ice_cfg()->is_rotation_locked)
    {
        return NO;
    }
    if (g_is_in_player_now) 
    {
       
        return [bg_view.player_view_controller shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    }
	return YES;
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    if (g_is_in_player_now)
    {
        [bg_view.player_view_controller didRotateFromInterfaceOrientation:fromInterfaceOrientation];
    }
    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
}
//*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	
	// Remove all subviews from super view
    [bg_view.player_view_controller.view removeFromSuperview];
	[bg_view removeFromSuperview];
    if (psw)
    {
        [psw release];
        psw = NULL;
    }
}


- (void)dealloc {
    //stop web server
    if (g_web_server_handle)
    {
        web_server_stop(g_web_server_handle);
        g_web_server_handle = NULL;
        
        web_server_mg_set_redir_path(NULL, NULL);
    }
    
    [super dealloc];
	
	// Release all subviews
	[bg_view release];
}



- (IBAction)On_Put_Lock_Btn:(id)sender
{
    [psw On_Push_Lock_Btn];
}

-(IBAction)On_Set_Enable_Roatation:(id)sender
{
    gf_get_ice_cfg()->is_rotation_locked = bg_view.configure_view.swc_rotation.on ? false: true;
    gf_set_ice_cfg(gf_get_ice_cfg());
}

-(IBAction)On_Set_Enable_Subtitle:(id)sender
{
    gf_get_ice_cfg()->enable_subtitle = bg_view.configure_view.swc_subtitle.on ? true : false;
    gf_set_ice_cfg(gf_get_ice_cfg());
}

-(IBAction)On_Set_Enable_Text_Info:(id)sender
{
    gf_get_ice_cfg()->enable_text_info = bg_view.configure_view.swc_text_info.on ? true : false;
    [ICE_Hidable_Label_View Enable_Hide_All:!gf_get_ice_cfg()->enable_text_info];
    gf_set_ice_cfg(gf_get_ice_cfg());
}

-(IBAction)On_Put_Press_Me_Btn:(id)sender
{
    gf_get_ice_cfg()->loop_mode = (gf_get_ice_cfg()->loop_mode + 1)%E_LM_COUNT;
    E_LOOP_MODE lm = gf_get_ice_cfg()->loop_mode;
    switch (lm)
    {
        case E_LM_NO_LOOP:
            bg_view.configure_view.loop_mode_btn.state = E_PLM_NO_LOOP;
            bg_view.configure_view.lbl_loop_mode_state.text = IGS(EG_STR_ID_LOOP_MODE_NO_LOOP);
            bg_view.player_view_controller.p_player_view.p_g_lm_lbl.text = IGS(EG_STR_ID_LOOP_MODE_NO_LOOP);
            break;
        case E_LM_SINGLE_LOOP:
            bg_view.configure_view.loop_mode_btn.state = E_PLM_SINGLE_LOOP;
            bg_view.configure_view.lbl_loop_mode_state.text = IGS(EG_STR_ID_LOOP_MODE_SINGLE_LOOP);
            bg_view.player_view_controller.p_player_view.p_g_lm_lbl.text = IGS(EG_STR_ID_LOOP_MODE_SINGLE_LOOP);
            break;
        case E_LM_LIST_LOOP:
            bg_view.configure_view.loop_mode_btn.state = E_PLM_LIST_LOOP;
            bg_view.configure_view.lbl_loop_mode_state.text = IGS(EG_STR_ID_LOOP_MODE_LIST_LOOP);
            bg_view.player_view_controller.p_player_view.p_g_lm_lbl.text = IGS(EG_STR_ID_LOOP_MODE_LIST_LOOP);
            break;
        case E_LM_RANDOM:
            bg_view.configure_view.loop_mode_btn.state = E_PLM_RANDOM;
            bg_view.configure_view.lbl_loop_mode_state.text = IGS(EG_STR_ID_LOOP_MODE_RANDOM);
            bg_view.player_view_controller.p_player_view.p_g_lm_lbl.text = IGS(EG_STR_ID_LOOP_MODE_RANDOM);
            break;
        default:
            break;
    }
    gf_set_ice_cfg(gf_get_ice_cfg());
}

-(IBAction)On_Put_i_Btn:(id)sender
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://ftp-monkey-bad.uu8.dnsnd.com"]];
}

-(void)Restart_Web_Server
{
    if (g_web_server_handle)
    {
        web_server_stop(g_web_server_handle);
        g_web_server_handle = NULL;
    }
    g_web_server_handle = web_server_start(g_web_server_html_path, NULL);
}



@end
